<template>
	<div>
		<AplidForm ref="formRef" v-bind="formConfig" v-model="formModel" @submit="onSubmit"> </AplidForm>
	</div>
</template>

<script setup lang="ts">
import apiMod from '@/apiMod'
import { formConfig } from '../config'
// import { useCommonStore } from '@/stores/common'
import { cloneDeep } from 'lodash-es'
let emit = defineEmits(['refresh'])
import { EncodeUtf8 } from '@/views/oneStopOffice/utils/index'
import { getAreaObj } from '@/utils/util'
// import { useOperate } from '@/views/datacenter/hooks/useConfig'

type Props = {
	nowRowData?: any
}
let props = withDefaults(defineProps<Props>(), {
	nowRowData: {},
})

// const emit = defineEmits(['refresh'])
let handleData = props.nowRowData
if(handleData.adaptiveHierarchyArea){
	handleData.adaptiveHierarchy = [handleData.adaptiveHierarchyCity,handleData.adaptiveHierarchyArea]
}else{
	handleData.adaptiveHierarchy = handleData.adaptiveHierarchyCity
}

const formModel = ref({
	...cloneDeep(handleData),
})
// 深拷贝一份
let data = cloneDeep(props.nowRowData)
if (data.policyKeywords && Array.isArray(data.policyKeywords)) {
	data.policyKeywords = data.policyKeywords.join(',')
}
if (data.adaptiveHierarchy && Array.isArray(data.adaptiveHierarchy)) {
	data.adaptiveHierarchy = data.adaptiveHierarchy.join(',')
}
let baseData = Object.freeze(JSON.parse(JSON.stringify(data)))

const formRef = ref()
const onSubmit = (params, done) => {
	if (params.policyKeywords && Array.isArray(params.policyKeywords)) {
		params.policyKeywords = params.policyKeywords.join(',')
	}
	// if (params.policyContent) {
	// 	params.policyContent = EncodeUtf8(params.policyContent)
	// }
	if(params.ageRange && params.ageRange!=''){
			let list = params.ageRange.split(',')
			params.minAge = list[0] == ''?undefined:list[0]
			params.maxAge = list[1] == ''?undefined:list[1]
		}else{
			params.minAge = undefined
			params.maxAge = undefined
		}
	const areasObj: any = getAreaObj(params.areas, 'from')
	const areasNameObj: any = getAreaObj(params.areasName, 'from')

	params.policyLevel = 1
	params.policyLevelLabel = '国家级'
	// params.module = '1'
	// params.moduleLabel = '一站办'
	// 一站办处理
	// 输入框都进行一下加密
	// formConfig.column.forEach(el=>{
    //     if((!el.type||el.type=='input'||el.type=="text")&& params[el.prop]){
    //       params[el.prop] =  EncodeUtf8(params[el.prop])
    //     }
    //   })
	if(Array.isArray(params.adaptiveHierarchy) && params.adaptiveHierarchy.length>0){
			if(params.adaptiveHierarchy.length==1){
				params.adaptiveHierarchyCity = params.adaptiveHierarchy[0]
				params.adaptiveHierarchyArea = ''
			}else{
				params.adaptiveHierarchyCity = params.adaptiveHierarchy[0]
				params.adaptiveHierarchyArea = params.adaptiveHierarchy[1]
			}
			params.adaptiveHierarchy = params.adaptiveHierarchy.join(',')
		}
	if (params.adaptiveHierarchyLabel) {
		params.adaptiveHierarchy = params.adaptiveHierarchy.join(',')
		const temp = params.adaptiveHierarchyLabel.split('/')
		params.adaptiveHierarchyLabel = temp[temp.length - 1]
	}
	const changeObj = {
		data: [],
		dataChangeContentList: [
			{
				changeBefore: {
					...baseData,
					areas: undefined,
					areasName: undefined,
					code: undefined,
				},
				changeAfter: {
					...params,
					...areasObj,
					...areasNameObj,
					areas: undefined,
					areasName: undefined,
					fromCommunityId:undefined,
					fromCommunityName:undefined,
					fromStreetId:undefined,
					fromStreetName:undefined,
					code: undefined,
				},
				changeData: {
					...params,
					...areasObj,
					...areasNameObj,
					areas: undefined,
					areasName: undefined,
					fromCommunityId:undefined,
					fromStreetId:undefined,
					fromCommunityName:undefined,
					fromStreetName:undefined,
					code: undefined,
				},
				changeType: '养老资讯数据',
				code: 3011,
			},
		],
	}
	// 调取变更
	apiMod.view_dpDataChange_add(changeObj).then((res) => {
		done()
		emit('refresh')
	})
}
const onSave = () => {
	formRef.value!.onSubmit()
}
defineExpose({
	onSave,
})
</script>

<style scoped></style>
